import { Component, OnInit } from '@angular/core';
import { NavParams, NavController, ModalController } from '@ionic/angular';
import { InstallationPage } from '../installation/installation';
import { InstallService } from '@services/data.service/index';
import { MyNoteService } from '@services/myNote.service';
import { EmitService } from '@services/emit.service';
import { ActivatedRoute } from '@angular/router';

@Component({
    selector: 'installSelect',
    templateUrl: 'installSelect.html',
    styleUrls: ['installSelect.scss']
})

export class InstallSelectPage implements OnInit {
    type: string;
    deviceList = [];    // 设备列表
    roomInfo;
    Style;
    ConInfo: string;
    meterLevel = null;  // 默认选择的选项
    meterStatus;  // 参数选择项
    checkStatu = [];  // 共用状态


    constructor(
        public navCtrl: NavController,
        public modalCtrl: ModalController,
        private installService: InstallService,
        public emitService: EmitService,
        private myNote: MyNoteService,
        private router: ActivatedRoute
    ) {
        this.router.queryParams.subscribe(data => {
            this.type = data.meterType;
            console.log('安装仪表类型', this.type);
        })
    }

    ngOnInit() {
        this.roomInfo = this.emitService.installParams;
        console.log('父仪表界面房间信息', this.roomInfo)
        if (this.emitService.installParams.mapping) {
            this.meterLevel = this.emitService.installParams.meterLevel || null;
        }

        // 如果是PIO类型仪表则获取设备列表
        if (this.type === 'isPIOM') {
            this.deviceList = this.roomInfo.device.deviceList;
            let State = [];
            if (this.roomInfo.device.meterModelId === 1007) {
                State = [
                    { stateId: 24, stateName: '启停' },
                    { stateId: 18, stateName: '故障' },
                    { stateId: 28, stateName: '手自动' },
                ]
            } else {
                State = [
                    { stateId: 24, stateName: '启停' },
                    { stateId: 18, stateName: '故障' },
                    { stateId: 28, stateName: '手自动' },
                    { stateId: 51, stateName: '工频' },
                    { stateId: 52, stateName: '变频' },
                ]
            }

            for (const data of this.deviceList) {
                data.stateList = [];
                data.stateNum = [];
                const arr = [];
                const arrNum = [];
                for (const item of data.parameterIds) {
                    for (const val of State) {
                        if (val.stateId === item) {
                            arr.push({ sTrue: false, pName: val.stateName, disabled: false });
                            arrNum.push(val.stateName);
                        }
                    }
                }
                data.stateList = arr;
                data.stateNum = arrNum;
            }

            const arrStatus = [];
            if (this.deviceList[0] && this.deviceList[0].stateNum && this.deviceList[1] && this.deviceList[1].stateNum) {
                for (const data of this.getArrEqual(this.deviceList[0].stateNum, this.deviceList[1].stateNum)) {
                    if (data !== '启停') {
                        arrStatus.push({ name: data, state: false })
                    }
                }
            }
            this.checkStatu = arrStatus;
            console.log('设备参数', this.deviceList);
            console.log('共用参数', this.checkStatu);
            this.meterStatus = [{
                title: '监测对象:',
                Monitor: {
                    deviceId: this.deviceList && this.deviceList[0] && this.deviceList[0].deviceId ? this.deviceList[0].deviceId : null,
                    deviceTypeId: this.deviceList && this.deviceList[0] && this.deviceList[0].deviceTypeId ? this.deviceList[0].deviceTypeId : null,
                    deviceName: this.deviceList && this.deviceList[0] && this.deviceList[0].deviceName ? this.deviceList[0].deviceName : null,
                    stateList: this.deviceList && this.deviceList[0] && this.deviceList[0].stateList ? this.deviceList[0].stateList : null
                }
            }, {
                title: '监测对象:',
                Monitor: {
                    deviceId: this.deviceList && this.deviceList[1] && this.deviceList[1].deviceId ? this.deviceList[1].deviceId : null,
                    deviceTypeId: this.deviceList && this.deviceList[1] && this.deviceList[1].deviceTypeId ? this.deviceList[1].deviceTypeId : null,
                    deviceName: this.deviceList && this.deviceList[1] && this.deviceList[1].deviceName ? this.deviceList[1].deviceName : null,
                    stateList: this.deviceList && this.deviceList[1] && this.deviceList[1].stateList ? this.deviceList[1].stateList : null
                }
            }];
            console.log(this.meterStatus)
        }

        // 如果是子仪表则获取父仪表列表
        if (this.type === 'isChildSensor') {
            this.Style = {
                title: '所属采集器',
                dataValueList: [],
                defaultOps: this.roomInfo.meterLevel || '',
            };
            this.installService.getCollectors(this.emitService.installParams.device.meterId).subscribe(res => {
                let firstData;
                if (res.success) {
                    for (let i = 0; i < res.data.collectors.length; i++) {
                        res.data.collectors[i].id = res.data.collectors[i].meterId;
                        res.data.collectors[i].name = res.data.collectors[i].meterName;
                    }
                    if (this.Style.defaultOps) {
                        firstData = this.Style.defaultOps;
                    } else {
                        firstData = res.data.collectors[0];
                    }
                }
                this.meterLevel = { name: firstData.name, id: firstData.id };
                this.Style.defaultOps = firstData;
                this.Style.dataValueList = res.data.collectors;
                this.roomInfo.device.pid = firstData.meterId;
            });
        }
    }

    ionViewWillEnter() {
    }

    remove(arr, val) {
        const index = arr.indexOf(val);
        if (index > -1) {
            arr.splice(index, 1);
        }
    };

    getArrEqual(arr1, arr2) {
        const newArr = [];
        for (let i = 0; i < arr2.length; i++) {
            for (let j = 0; j < arr1.length; j++) {
                if (arr1[j] === arr2[i]) {
                    newArr.push(arr1[j]);
                }
            }
        }
        return newArr;
    }

    // 选择共用状态
    checkStatus(datas, i) {
        datas.state = !datas.state;
        for (const data of this.meterStatus) {
            for (const item of data.Monitor.stateList) {
                if (item.pName === datas.name) {
                    if (datas.state) {
                        item.sTrue = false;
                        item.disabled = true;
                    } else {
                        item.disabled = false;
                    }
                }
            }
        }
    }

    begin() {
        const dataArray = {
            deviceParams: [],
            addtions: []
        };
        switch (this.type) {
            case 'isPIOM':
                for (const data of this.meterStatus) {
                    data.Monitor.deviceId ? dataArray.deviceParams.push(data.Monitor) : null;
                }
                for (const data of this.checkStatu) {
                    dataArray.addtions.push(data);
                }
                dataArray.deviceParams
                this.roomInfo.Relation = dataArray;
                this.roomInfo.device.Relation = dataArray;
                this.emitService.installParams = this.roomInfo;
                this.navCtrl.navigateForward(['tabs/my/InstallationPage']);
                break;
            case 'isChildSensor':
                if (this.meterLevel) {
                    this.roomInfo.meterLevel = this.meterLevel;
                    this.roomInfo.device.meterLevel = this.meterLevel;
                    this.roomInfo.device.pid = this.meterLevel.id;
                    this.emitService.installParams = this.roomInfo;
                    this.navCtrl.navigateForward(['tabs/my/InstallationPage']);
                } else {
                    this.myNote.MyNotice('请选择采集器');
                };
                break
        }
    }

    selectedObj(e) {
        if (e) {
            this.meterLevel = { name: e.name, id: e.meterId };
        }
    }
}
